import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by Scott Tiger.
 * User: tomaszsocha
 * Date: 2010-05-07
 * Time: 15:25:56
 */
public class Polaczenia {
    public HashMap<String,Object> laczenia;
    String nodes[] = {"Armor","Helmet","Pants","Ring","Necklace"};
    

    public Polaczenia() {
        laczenia= new HashMap<String,Object>();
        try {
            for (String val : nodes) {
            laczenia = processLineByLine(val,laczenia);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


    public  HashMap processLineByLine(String itemName,HashMap polaczenia) throws FileNotFoundException {
    String fileLocation = "bw/"+itemName+"Prefy.csv";
    HashMap <String,Object> laczenia = new HashMap<String,Object>();
    Scanner scanner = null;
    try {
        InputStream ms = getClass().getClassLoader().getResourceAsStream(fileLocation);
     scanner = new Scanner(ms,"UTF-8");

      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          String result = line.substring(0, line.indexOf(","));
          String param2 = line.substring(line.indexOf(",")+1,line.indexOf(",",line.indexOf(",")+1));
          String param1 = line.substring(line.indexOf(",",line.indexOf(",")+1)+1);
          HashMap<String,String> o = new HashMap<String,String>();
          if (laczenia.containsKey(param1)) {
          o = (HashMap<String,String>) laczenia.get(param1);}
          o.put(param2,result);
          laczenia.put(param1,o);
      }
      fileLocation = "bw/"+itemName+"Sufy.csv";
       ms = getClass().getClassLoader().getResourceAsStream(fileLocation);
      scanner = new Scanner(ms,"UTF-8");

      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          String result = line.substring(0, line.indexOf(","));
          String param2 = line.substring(line.indexOf(",")+1,line.indexOf(",",line.indexOf(",")+1));
          String param1 = line.substring(line.indexOf(",",line.indexOf(",")+1)+1);
          HashMap<String,String> o = new HashMap<String,String>();
          if (laczenia.containsKey(param1)) {
          o = (HashMap<String,String>) laczenia.get(param1);}
          o.put(param2,result);
          laczenia.put(param1,o);
      }

      fileLocation = "bw/"+itemName+".csv";
      ms = getClass().getClassLoader().getResourceAsStream(fileLocation);
      
      scanner = new Scanner(ms,"UTF-8");


      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          String result = line.substring(0, line.indexOf(","));
          String param2 = line.substring(line.indexOf(",")+1,line.indexOf(",",line.indexOf(",")+1));
          String param1 = line.substring(line.indexOf(",",line.indexOf(",")+1)+1);
          HashMap<String,String> o = new HashMap<String,String>();
          if (laczenia.containsKey(param1)) {
          o = (HashMap<String,String>) laczenia.get(param1);}
          o.put(param2,result);
          laczenia.put(param1,o);
      }
    }
    finally {
      //ensure the underlying stream is always closed
        System.out.println(System.getProperty("user.home"));
      scanner.close();
    }
        polaczenia.put(itemName,laczenia);
        return polaczenia;
  }

    public String polacz(String pref1,String pref2,String item){
       if (item.equals("Armor"))
       return polacz(pref1,pref2);
       return (String) ((HashMap) ((HashMap) laczenia.get(item)).get(pref1)).get(pref2);

    }

    public String polacz(String pref1,String pref2){
       String item = "Armor";
       String pre1 = pref1.substring(0,pref1.indexOf(" "));
       String pre2 = pref2.substring(0,pref2.indexOf(" "));
       String suf1 = pref1.substring(pref1.indexOf(" ")+1);
       String suf2 = pref2.substring(pref2.indexOf(" ")+1);
       return ((HashMap) ((HashMap) laczenia.get(item)).get(pre1)).get(pre2)+ " " + ((HashMap) ((HashMap) laczenia.get(item)).get(suf1)).get(suf2);

    }


}
